package cascade.components
{
	
	import flash.text.TextFormat;
	
	import flight.utils.ColorMatrix;
	
	import mx.controls.treeClasses.TreeItemRenderer;
	
	[Style(name="horizontalGap", type="int", format="Length", inherit="no")]
	
	public class SiteTreeItemRenderer extends TreeItemRenderer
	{
		
		protected override function measure():void
		{
			super.measure();
			
			var gap:int = getStyle("horizontalGap") || 0;
			if (disclosureIcon)
				measuredWidth += gap;
			
			if (icon)
				measuredWidth += gap;
		}
		
		
		protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
	
			var gap:int = getStyle("horizontalGap") || 0;
			
			if (disclosureIcon)
			{
				if (icon)
					icon.x += gap;
				label.x += gap;
			}
			
			if (icon)
			{
				label.x += gap;
			}
			
			label.setActualSize(Math.min(label.measuredWidth, unscaledWidth - label.x), label.measuredHeight);
			
			var verticalAlign:String = getStyle("verticalAlign");
			if (verticalAlign == "middle")
				label.y = (unscaledHeight - label.height) / 2;
			else if (verticalAlign == "bottom")
				label.y = unscaledHeight - label.height + 2; // 2 for gutter
			
			if (data && parent)
			{
				var owner:SiteTree = listData.owner as SiteTree;
				var tf:TextFormat = new TextFormat();
				tf.underline = owner.isItemHighlighted(data);
				if (owner.isItemSelected(data))
					tf.color = getStyle("textSelectedColor");
				label.setTextFormat(tf);
			}
		}
		
		public function setBackgroundColor(color:Number):void
		{
			if (isNaN(color))
			{
				label.background = false;
				if (icon)
					icon.filters = [];
			}
			else
			{
				label.background = true;
				label.backgroundColor = color;
				
				if (data && parent)
				{
					var owner:SiteTree = listData.owner as SiteTree;
					if (icon && owner.isItemSelected(listData.uid))
					{
						var m:ColorMatrix = new ColorMatrix();
						m.colorize(color, .5);
						icon.filters = [m.filter];
					}
					invalidateDisplayList();
				}
			}
		}
	}
}
